{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Approve Hits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import boto3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "aws_access_key_id = \"AKIAJ7ODKKKLEQNUJYZA\"\n",
    "aws_secret_access_key = \"XXXXX\"\n",
    "aws_region = \"us-east-1\"\n",
    "client = boto3.client('mturk', region_name= \"us-east-1\", aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ResponseMetadata': {'RequestId': '5c6b81ce-fc0c-4125-a1c2-bfc4d157bfa9',\n",
       "  'HTTPStatusCode': 200,\n",
       "  'HTTPHeaders': {'x-amzn-requestid': '5c6b81ce-fc0c-4125-a1c2-bfc4d157bfa9',\n",
       "   'content-type': 'application/x-amz-json-1.1',\n",
       "   'content-length': '2',\n",
       "   'date': 'Sun, 03 Mar 2019 01:04:12 GMT'},\n",
       "  'RetryAttempts': 0}}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "client.approve_assignment(AssignmentId=\"3QFUFYSY9ZFOQSZK8PZXFP84UVOF4N\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'NextToken': 'p1:thjV0Iu9KED6Jtn5ylDyJ+bUn3ooStDrFP8Z1CmhpFWeCbNFBveoYz1nm2qSsQ==',\n",
       " 'NumResults': 1,\n",
       " 'Assignments': [{'AssignmentId': '3QFUFYSY9ZFOQSZK8PZXFP84UVOF4N',\n",
       "   'WorkerId': 'A19H44LQJ0TEXF',\n",
       "   'HITId': '3R0WOCG21N9NNMH96NQNG98SB7WUDM',\n",
       "   'AssignmentStatus': 'Approved',\n",
       "   'AutoApprovalTime': datetime.datetime(2019, 4, 1, 15, 56, 22, tzinfo=tzlocal()),\n",
       "   'AcceptTime': datetime.datetime(2019, 3, 2, 14, 51, 24, tzinfo=tzlocal()),\n",
       "   'SubmitTime': datetime.datetime(2019, 3, 2, 14, 56, 22, tzinfo=tzlocal()),\n",
       "   'ApprovalTime': datetime.datetime(2019, 3, 2, 17, 4, 12, tzinfo=tzlocal()),\n",
       "   'Answer': '<?xml version=\"1.0\" encoding=\"ASCII\"?><QuestionFormAnswers xmlns=\"http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionFormAnswers.xsd\"><Answer><QuestionIdentifier>workerId</QuestionIdentifier><FreeText>A19H44LQJ0TEXF</FreeText></Answer></QuestionFormAnswers>'}],\n",
       " 'ResponseMetadata': {'RequestId': 'c20c08c1-c74f-4fdb-a170-78a45f8b7369',\n",
       "  'HTTPStatusCode': 200,\n",
       "  'HTTPHeaders': {'x-amzn-requestid': 'c20c08c1-c74f-4fdb-a170-78a45f8b7369',\n",
       "   'content-type': 'application/x-amz-json-1.1',\n",
       "   'content-length': '687',\n",
       "   'date': 'Sun, 03 Mar 2019 01:04:29 GMT'},\n",
       "  'RetryAttempts': 0}}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "client.list_assignments_for_hit(HITId='3R0WOCG21N9NNMH96NQNG98SB7WUDM', AssignmentStatuses=['Submitted'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def approve_all_submitted_assignments(client):\n",
    "    for id in [h['HITId'] for h in client.list_hits()['HITs']]:\n",
    "        lst = client.list_assignments_for_hit(HITId=id, AssignmentStatuses=['Submitted'])\n",
    "        for aid in [a[\"AssignmentId\"] for a in lst[\"Assignments\"]]:\n",
    "            client.approve_assignment(AssignmentId=aid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "approve_all_submitted_assignments(client)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
